export class MessageVO {
  role: MessageRoleEnum
  message: string
  timestamp: number
  id: string

  constructor(role: MessageRoleEnum, message: string, timestamp?: number, id?: string) {
    this.role = role
    this.message = message
    this.timestamp = timestamp || Date.now()
    this.id = id || this.generateId()
  }

  private generateId(): string {
    return Date.now().toString() + Math.random().toString(36).substr(2, 9)
  }
}

export class ConversationVO {
  id: string
  title: string
  messages: MessageVO[]
  createTime: number
  updateTime: number

  constructor(id?: string, title?: string, messages?: MessageVO[]) {
    this.id = id || this.generateId()
    this.title = title || '新对话'
    this.messages = messages || []
    this.createTime = Date.now()
    this.updateTime = Date.now()
  }

  private generateId(): string {
    return Date.now().toString() + Math.random().toString(36).substr(2, 9)
  }

  addMessage(message: MessageVO) {
    this.messages.push(message)
    this.updateTime = Date.now()
    // 更新对话标题为第一条用户消息的前20个字符
    if (this.messages.length === 1 && message.role === MessageRoleEnum.Mine) {
      this.title = message.message.substring(0, 20) + (message.message.length > 20 ? '...' : '')
    }
  }
}

export enum MessageRoleEnum {
  // 我方
  Mine,
  // 对方
  Other
}